package com.huika.dealontop.control.shops.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.huika.dealontop.R;
import com.huika.dealontop.control.base.extend.BaseViewHolder;
import com.huika.dealontop.support.domain.OtherProductItemBean;
import com.huika.dealontop.support.domain.ProductEvaluationItemBean;
import com.huika.dealontop.utils.DateTimeTool;
import com.huika.dealontop.utils.DistanceUtils;
import com.huika.dealontop.utils.ImageLoaderHelper;
import com.huika.dealontop.utils.UIUtils;
import com.huika.dealontop.views.RoundImageView;
import com.huika.huikalib.utils.MoneyShowTool;
import com.huika.huikalib.utils.StringUtils;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Author: Victor Wu <br/>
 * Email: victor-wu*foxmail.com <br/>
 * Time: 2015/11/26 20:46 <br/>
 * Description: 商品详情中 其他团购列表 和 评论列表数据的适配器
 */
public class CommAdapter extends BaseAdapter{

    public static int PRODUCT = 0;
    public static int EVALUATION = 1;
    public static int NOTES = 2;
    /**
     * 显示的数据类型
     */
    private int type = PRODUCT;

    private Context mContext;

    private List<ProductEvaluationItemBean> mEvaluationList;
    private List<OtherProductItemBean> mProductList;

    public CommAdapter(Context context){
        mContext = context;
    }

    @Override
    public int getCount() {
        if(type == PRODUCT){
            if(mProductList != null){
                return mProductList.size();
            }
        }else if(type == EVALUATION){
            if(mEvaluationList != null){
                return mEvaluationList.size();
            }
        }
        return 0;
    }

    public int getProductCount() {
        if (mProductList != null) {
            return mProductList.size();
        }
        return 0;
    }

    public int getEvaluationCount() {
        if (mEvaluationList != null) {
            return mEvaluationList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        if(type == PRODUCT){
            if(mProductList != null && mProductList.size() > i){
                return mProductList.get(i);
            }
        }else if(type == EVALUATION){
            if(mEvaluationList != null && mEvaluationList.size() > i){
                return mEvaluationList.get(i);
            }
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        CommHolder holder = null;
        if(view == null){
            holder = new CommHolder(mContext);
            view = holder.getRootView();
        }else{
            holder = (CommHolder) view.getTag();
        }
        holder.setData(getItem(i));
        return view;
    }

    public List<ProductEvaluationItemBean> getEvaluationList() {
        return mEvaluationList;
    }

    public void setEvaluationList(List<ProductEvaluationItemBean> evaluationList) {
        this.mEvaluationList = evaluationList;
        if(type == EVALUATION){
            notifyDataSetChanged();
        }
    }
    public void addEvaluationList(List<ProductEvaluationItemBean> evaluationList) {
        if (mEvaluationList != null) {
            mEvaluationList.addAll(evaluationList);
            if(type == EVALUATION){
                notifyDataSetChanged();
            }
        }else{
            setEvaluationList(evaluationList);
        }
    }

    public List<OtherProductItemBean> getProductList() {
        return mProductList;
    }

    public void setProductList(List<OtherProductItemBean> productList) {
        this.mProductList = productList;
        if(type == PRODUCT){
            notifyDataSetChanged();
        }
    }
    public void addProductList(List<OtherProductItemBean> productList) {
        if (mProductList != null) {
            mProductList.addAll(productList);
            if(type == PRODUCT){
                notifyDataSetChanged();
            }
        }else{
            setProductList(productList);
        }
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    class CommHolder extends BaseViewHolder{

        private LinearLayout mLlProductLayout;
        private LinearLayout mLlEvaluateLayout;

        //团购条目
        private ImageView mIvProductIcon;
        private TextView mTvProductName;
        private TextView mTvProductJunLi;
        private TextView mTvProductPrice;
        private TextView mTvProductDiscount;
        private RatingBar mRbProductScore;
        private TextView mTvProductScore;

        //评价条目
        private RoundImageView mIvUserIcon;
        private TextView mTvUserName;
        private RatingBar mRbEvaluateScore;
        private TextView mTvEvaluateScore;
        private TextView mTvEvaluateTime;
        private TextView mTvEvaluateContent;

        public CommHolder(Context context) {
            super(context);
        }

        @Override
        protected View initView() {
            return View.inflate(UIUtils.getContext(), R.layout.item_product_evaluate_layout, null);
        }

        @Override
        protected void assignView(View rootView) {
            mLlProductLayout = (LinearLayout) findViewById(R.id.ll_product_item);
            mLlEvaluateLayout = (LinearLayout) findViewById(R.id.ll_evaluate_item);

            mIvProductIcon = (ImageView) findViewById(R.id.project_iv_icon);
            mTvProductName = (TextView) findViewById(R.id.project_tv_name);
            mTvProductJunLi = (TextView) findViewById(R.id.project_tv_juli);
            mTvProductPrice = (TextView) findViewById(R.id.project_tv_price);
            mTvProductDiscount = (TextView) findViewById(R.id.project_tv_zhekounum);
            mRbProductScore = (RatingBar) findViewById(R.id.project_rb_pingfen);
            mTvProductScore = (TextView) findViewById(R.id.project_tv_pingfennum);

            mIvUserIcon = (RoundImageView) findViewById(R.id.iv_evaluation_user_icon);
            mTvUserName = (TextView) findViewById(R.id.tv_evaluation_user_name);
            mRbEvaluateScore = (RatingBar) findViewById(R.id.rb_evaluation_score);
            mTvEvaluateScore = (TextView) findViewById(R.id.tv_evaluation_score);
            mTvEvaluateTime = (TextView) findViewById(R.id.tv_evaluation_time);
            mTvEvaluateContent = (TextView) findViewById(R.id.tv_evaluation_content);
        }

        @Override
        protected void refreshView(Object data) {
            if(data ==  null){
                return;
            }

            if(type == EVALUATION){
                if(mLlEvaluateLayout.getVisibility() != View.VISIBLE){
                    mLlEvaluateLayout.setVisibility(View.VISIBLE);
                }
                if(mLlProductLayout.getVisibility() != View.GONE){
                    mLlProductLayout.setVisibility(View.GONE);
                }
                ProductEvaluationItemBean evaluation = (ProductEvaluationItemBean) data;
                ImageLoaderHelper.displayImage(evaluation.getHeadImage(), mIvUserIcon, R.mipmap.my_infomation_user_icon);
                mTvUserName.setText(evaluation.getNickName());
                mRbEvaluateScore.setRating(Float.parseFloat(MoneyShowTool.formatOne(evaluation.getEvalScore())));
                mTvEvaluateScore.setText(MoneyShowTool.formatOne(evaluation.getEvalScore()));
                mTvEvaluateTime.setText(DateTimeTool.dmyDate(evaluation.getEvalTime() * 1000));
                mTvEvaluateContent.setText(evaluation.getEvalContent());

            }else if(type == PRODUCT){
                if(mLlProductLayout.getVisibility() != View.VISIBLE){
                    mLlProductLayout.setVisibility(View.VISIBLE);
                }
                if(mLlEvaluateLayout.getVisibility() != View.GONE){
                    mLlEvaluateLayout.setVisibility(View.GONE);
                }
                OtherProductItemBean product = (OtherProductItemBean) data;
                ImageLoaderHelper.displayImage(product.getProductPicUrl(), mIvProductIcon, R.mipmap.morenpic);
                mTvProductName.setText(product.getProductName());
                //mTvProductJunLi.setText(DistanceUtils.formatMeter(product.getProductRate()));
                mTvProductPrice.setText(MoneyShowTool.nolastDF(product.getSalesPrice()));
                if(product.getDiscount() > 0){
                    mTvProductDiscount.setText(MoneyShowTool.nolastDF(product.getDiscount(), false) + "% Off");
                }
                if(product.getProductRate() == 0){
                    product.setProductRate(5);
                }
                mRbProductScore.setRating(Float.parseFloat(MoneyShowTool.formatOne(product.getProductRate())));
                mTvProductScore.setText(MoneyShowTool.formatOne(product.getProductRate()));
            }
        }
    }
}
